<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/facicon.ico?v=5.1.4" color="#222">





  <meta name="keywords" content="Java,">





  <link rel="alternate" href="/atom.xml" title="Key" type="application/atom+xml">






<meta name="description" content="链表链表的本质是一个动态的对象数组,他可以实现若干个对象的存储. 链表的基本定义在实际的开发之中对象数组是一项非常实用的技术,并且利用其可以描述出”多”方的概念,例如:一个人有多本书,则在人的类里面一定要提供有一个对象数组保存书的信息,但是传统的对象数组依赖于数组的概念,所以数组里面最大的缺点在于:长度是固定的,正是因为如此所以在实际的开发之中,传统的数组应用是非常有限的(数组的接受以及循环使用)">
<meta name="keywords" content="Java">
<meta property="og:type" content="article">
<meta property="og:title" content="链表">
<meta property="og:url" content="https://blog.qipo.net/2019/06/11/LinkedList/index.html">
<meta property="og:site_name" content="Key">
<meta property="og:description" content="链表链表的本质是一个动态的对象数组,他可以实现若干个对象的存储. 链表的基本定义在实际的开发之中对象数组是一项非常实用的技术,并且利用其可以描述出”多”方的概念,例如:一个人有多本书,则在人的类里面一定要提供有一个对象数组保存书的信息,但是传统的对象数组依赖于数组的概念,所以数组里面最大的缺点在于:长度是固定的,正是因为如此所以在实际的开发之中,传统的数组应用是非常有限的(数组的接受以及循环使用)">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://imgs.qipo.net/image/linkedlist/linkedList.jpg">
<meta property="og:image" content="http://imgs.qipo.net/image/linkedlist/linkedListStructure.jpg">
<meta property="og:image" content="http://imgs.qipo.net/image/linkedlist/toArray.jpg">
<meta property="og:image" content="http://imgs.qipo.net/image/linkedlist/getDataByIndex.png">
<meta property="og:image" content="http://imgs.qipo.net/image/linkedlist/deleteData.png">
<meta property="og:image" content="http://imgs.qipo.net/image/linkedlist/deleteData1.png">
<meta property="og:updated_time" content="2020-06-04T08:36:19.730Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="链表">
<meta name="twitter:description" content="链表链表的本质是一个动态的对象数组,他可以实现若干个对象的存储. 链表的基本定义在实际的开发之中对象数组是一项非常实用的技术,并且利用其可以描述出”多”方的概念,例如:一个人有多本书,则在人的类里面一定要提供有一个对象数组保存书的信息,但是传统的对象数组依赖于数组的概念,所以数组里面最大的缺点在于:长度是固定的,正是因为如此所以在实际的开发之中,传统的数组应用是非常有限的(数组的接受以及循环使用)">
<meta name="twitter:image" content="http://imgs.qipo.net/image/linkedlist/linkedList.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://blog.qipo.net/2019/06/11/LinkedList/">





  <title>链表 | Key</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Key</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://blog.qipo.net/2019/06/11/LinkedList/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Key">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Key">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">链表</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-11T10:41:13+08:00">
                2019-06-11
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  9.7k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  43
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="链表"><a href="#链表" class="headerlink" title="链表"></a>链表</h1><p>链表的本质是一个动态的对象数组,他可以实现若干个对象的存储.</p>
<h2 id="链表的基本定义"><a href="#链表的基本定义" class="headerlink" title="链表的基本定义"></a>链表的基本定义</h2><p>在实际的开发之中对象数组是一项非常实用的技术,并且利用其可以描述出”多”方的概念,例如:一个人有<br>多本书,则在人的类里面一定要提供有一个对象数组保存书的信息,但是传统的对象数组依赖于数组的概念,所以<br>数组里面最大的缺点在于:长度是固定的,正是因为如此所以在实际的开发之中,传统的数组应用是非常有限的(数组的接受<br>以及循环使用),但是如果要想进行灵活的数据保存,那么就必须自己来实现结构.</p>
<p>传统对象数组的开发操作依赖于脚标(索引)的控制,如果要想实现内容的动态维护,那么难度太高了,而且复杂度<br>攀升,所以现在可以发现,对于一成不动的数据可以使用对象数组来实现,但是对于可能随时变化的数据,就必须<br>实现一个可以动态扩充的对象数组.</p>
<p>所谓的链表实质性的本质是利用引用的逻辑关系来实现类似数组的数据处理操作,以一种保存”多”方数据的形式,<br>实现数组类似的功能.</p>
<p>如果先要实现链表的处理,那么需要有一个公共的结构,这个结构可以实现数据的保存以及下一个链接的指向,<br>为了描述这样的逻辑,可以把每一个存储理解为一个节点,所以此时应该准备出一个节点类,但是这个节点类里面可以<br>保存各种数据类型的数据.</p>
<p><img src="http://imgs.qipo.net/image/linkedlist/linkedList.jpg" alt></p>
<p>虽然已经清楚了需要通过Node节点来进行数据的保存,但是毕竟这里面需要牵扯到节点的引用处理关系,那么这个引用处理关系<br>是由使用者控制吗?这样肯定的不可能,所以应该有一个专门的类来进行节点的引用关系的配置.</p>
<p>范例:直接操作Node很麻烦</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> E data;</span><br><span class="line">    <span class="keyword">private</span> Node next;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Node</span><span class="params">(E data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.data = data;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">getData</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> data;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Node <span class="title">getNext</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> next;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNext</span><span class="params">(Node next)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.next = next;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Node&lt;String&gt; n1 = <span class="keyword">new</span> Node&lt;&gt;(<span class="string">"火车头"</span>);</span><br><span class="line">        Node&lt;String&gt; n2 = <span class="keyword">new</span> Node&lt;&gt;(<span class="string">"车厢1"</span>);</span><br><span class="line">        Node&lt;String&gt; n3 = <span class="keyword">new</span> Node&lt;&gt;(<span class="string">"车厢2"</span>);</span><br><span class="line">        Node&lt;String&gt; n4 = <span class="keyword">new</span> Node&lt;&gt;(<span class="string">"车厢4"</span>);</span><br><span class="line">        Node&lt;String&gt; n5 = <span class="keyword">new</span> Node&lt;&gt;(<span class="string">"车厢5"</span>);</span><br><span class="line">        n1.setNext(n2);</span><br><span class="line">        n2.setNext(n3);</span><br><span class="line">        n3.setNext(n4);</span><br><span class="line">        n4.setNext(n5);</span><br><span class="line">        print(n1);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">print</span><span class="params">(Node&lt;?&gt; node)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(node != <span class="keyword">null</span>) &#123;<span class="comment">// 有节点</span></span><br><span class="line">            System.out.println(node.getData());</span><br><span class="line">            print(node.getNext()); <span class="comment">// 递归调用</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这样肯定不可能,所以应该有一个专门的类进行节点的引用关系的配置.因为真实的使用者关心的<br>只是数据的存储与获取,所以现在应该对我们的Node类进行包装处理.</p>
</blockquote>
<p><img src="http://imgs.qipo.net/image/linkedlist/linkedListStructure.jpg" alt></p>
<h2 id="数据增加"><a href="#数据增加" class="headerlink" title="数据增加"></a>数据增加</h2><p>通过之前的分析可以发现在进行链表操作的过程之中为了避免转型的异常应该使用泛型,同时也应该<br>设计一个链表的标准接口,同时具体实现该接口的时候还应该通过Node类做出节点的关系描述.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123; <span class="comment">// 保存的节点的数据关系</span></span><br><span class="line">        <span class="keyword">private</span> E data; <span class="comment">//保存的数据</span></span><br><span class="line">        <span class="keyword">private</span> Node next; <span class="comment">// 保存的下一个引用</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span> <span class="params">(E data)</span> </span>&#123;  <span class="comment">// 有数据的情况下才有意义</span></span><br><span class="line">            <span class="keyword">this</span>.data = data;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的结构-----------------</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在现在所定义的Node类之中并没有出现有setter与getter方法,是因为内部类中的私有属性与方便外类类<br>直接访问.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123; <span class="comment">// 保存的节点的数据关系</span></span><br><span class="line">        <span class="keyword">private</span> E data; <span class="comment">//保存的数据</span></span><br><span class="line">        <span class="keyword">private</span> Node next; <span class="comment">// 保存的下一个引用</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span> <span class="params">(E data)</span> </span>&#123;  <span class="comment">// 有数据的情况下才有意义</span></span><br><span class="line">            <span class="keyword">this</span>.data = data;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 第一次调用: this = LinkImpl.root;</span></span><br><span class="line">        <span class="comment">// 第二次调用: this = LinkImpl.root.next;</span></span><br><span class="line">        <span class="comment">// 第三次调用: this = LinkImpl.root.next.next;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addNode</span> <span class="params">(Node newNode)</span> </span>&#123; <span class="comment">// 保存新的Node数据集</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>)&#123;  <span class="comment">// 当前节点的下一个节点为null</span></span><br><span class="line">                <span class="keyword">this</span>.next = newNode; <span class="comment">// 保存当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">this</span>.next.addNode(newNode);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的成员-----------------</span></span><br><span class="line">    <span class="keyword">private</span> Node root;  <span class="comment">// 根节点</span></span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的方法-----------------</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数据本身不具有关联特性,只有Node节点有,那么要实现关联就必须将数据包装在Node类中</span></span><br><span class="line">        Node node = <span class="keyword">new</span> Node(e); <span class="comment">// 创建一个新的节点</span></span><br><span class="line">        <span class="keyword">if</span>(root == <span class="keyword">null</span>) &#123; <span class="comment">// 现在没有根节点</span></span><br><span class="line">            <span class="keyword">this</span>.root = node; <span class="comment">// 第一个节点作为根节点</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.root.addNode(node); <span class="comment">// 将新节点保存在合适的位置</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">        ILink&lt;String&gt; all = <span class="keyword">new</span> LinkImpl&lt;&gt;();</span><br><span class="line">        all.add(<span class="string">"Hello"</span>);</span><br><span class="line">        all.add(<span class="string">"World"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>Link类只是负责数据的操作与节点的处理,而所有后续节点的处理都是由Node类负责完成的.</p>
<h2 id="获取集合个数"><a href="#获取集合个数" class="headerlink" title="获取集合个数"></a>获取集合个数</h2><p>在链表之中往往需要保存有大量的数据,那么这些数据往往需要进行数据个数的统计操作,所以应该在<br>LinkImpl里面追加有数据统计信息,同时当增加或删除数据时,有应该对个数进行修改.</p>
<blockquote>
<p>1.在ILink接口里面追加有一个获取数据个数的方法;</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>; <span class="comment">// 增加数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>; <span class="comment">// 获取数据的个数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>2.在LinkImpl子类里里里面追加一个个数统计的属性;</p>
</blockquote>
<figure class="highlight axapta"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> <span class="keyword">count</span>; <span class="comment">// 保存数据的个数</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>3.在add()方法里面进行数据个数的追加</p>
</blockquote>
<figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">@Override</span><br><span class="line">   public void add(E e) &#123;</span><br><span class="line">       // 数据本身不具有关联特性,只有<span class="keyword">Node</span><span class="title">节点有,那么要实现关联就必须将数据包装在Node</span>类中</span><br><span class="line">       <span class="keyword">Node</span> <span class="title">node</span> = new <span class="keyword">Node</span><span class="title">(e</span>); // 创建一个新的节点</span><br><span class="line">       if(root == null) &#123; // 现在没有根节点</span><br><span class="line">           this.root = <span class="keyword">node</span><span class="title">; // 第一个节点作为根节点</span></span><br><span class="line"><span class="title">       &#125;else</span>&#123;</span><br><span class="line">           this.root.addNode(<span class="keyword">node</span><span class="title">); // 将新节点保存在合适的位置</span></span><br><span class="line"><span class="title">       &#125;</span></span><br><span class="line"><span class="title">       this</span>.count ++;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>4.需要在LinkImpl子类里面来返回数据的个数</p>
</blockquote>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.count;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>完整代码</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>; <span class="comment">// 增加数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>; <span class="comment">// 获取数据的个数</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123; <span class="comment">// 保存的节点的数据关系</span></span><br><span class="line">        <span class="keyword">private</span> E data; <span class="comment">//保存的数据</span></span><br><span class="line">        <span class="keyword">private</span> Node next; <span class="comment">// 保存的下一个引用</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span> <span class="params">(E data)</span> </span>&#123;  <span class="comment">// 有数据的情况下才有意义</span></span><br><span class="line">            <span class="keyword">this</span>.data = data;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 第一次调用: this = LinkImpl.root;</span></span><br><span class="line">        <span class="comment">// 第二次调用: this = LinkImpl.root.next;</span></span><br><span class="line">        <span class="comment">// 第三次调用: this = LinkImpl.root.next.next;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addNode</span> <span class="params">(Node newNode)</span> </span>&#123; <span class="comment">// 保存新的Node数据集</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>)&#123;  <span class="comment">// 当前节点的下一个节点为null</span></span><br><span class="line">                <span class="keyword">this</span>.next = newNode; <span class="comment">// 保存当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">this</span>.next.addNode(newNode);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的成员-----------------</span></span><br><span class="line">    <span class="keyword">private</span> Node root;  <span class="comment">// 根节点</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> count; <span class="comment">// 保存数据的个数</span></span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的方法-----------------</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数据本身不具有关联特性,只有Node节点有,那么要实现关联就必须将数据包装在Node类中</span></span><br><span class="line">        Node node = <span class="keyword">new</span> Node(e); <span class="comment">// 创建一个新的节点</span></span><br><span class="line">        <span class="keyword">if</span>(root == <span class="keyword">null</span>) &#123; <span class="comment">// 现在没有根节点</span></span><br><span class="line">            <span class="keyword">this</span>.root = node; <span class="comment">// 第一个节点作为根节点</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.root.addNode(node); <span class="comment">// 将新节点保存在合适的位置</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.count ++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">        ILink&lt;String&gt; all = <span class="keyword">new</span> LinkImpl&lt;&gt;();</span><br><span class="line">        System.out.println(<span class="string">"[增加之前]数据的个数:"</span> + all.size());</span><br><span class="line">        all.add(<span class="string">"Hello"</span>);</span><br><span class="line">        all.add(<span class="string">"World"</span>);</span><br><span class="line">        System.out.println(<span class="string">"[增加之后]数据的个数:"</span> + all.size());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="空集合判断"><a href="#空集合判断" class="headerlink" title="空集合判断"></a>空集合判断</h2><p>链表里面可以保存有若干个数据,如果说现在链表还没有保存数据,则表示是一个空集合,则应该提供有一个空的判断.</p>
<blockquote>
<p>1.在ILink接口里面追加判断方法;</p>
</blockquote>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>; <span class="comment">// 判断是否为空集合</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>2.在LinkImpl中腹泻此方法;</p>
</blockquote>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// return this.root == null;</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.count == <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>使用根节点或者长度判断其本质是一样的.</p>
<blockquote>
<p>完整代码</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>; <span class="comment">// 增加数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>; <span class="comment">// 获取数据的个数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>; <span class="comment">// 判断是否为空集合</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123; <span class="comment">// 保存的节点的数据关系</span></span><br><span class="line">        <span class="keyword">private</span> E data; <span class="comment">//保存的数据</span></span><br><span class="line">        <span class="keyword">private</span> Node next; <span class="comment">// 保存的下一个引用</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span> <span class="params">(E data)</span> </span>&#123;  <span class="comment">// 有数据的情况下才有意义</span></span><br><span class="line">            <span class="keyword">this</span>.data = data;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 第一次调用: this = LinkImpl.root;</span></span><br><span class="line">        <span class="comment">// 第二次调用: this = LinkImpl.root.next;</span></span><br><span class="line">        <span class="comment">// 第三次调用: this = LinkImpl.root.next.next;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addNode</span> <span class="params">(Node newNode)</span> </span>&#123; <span class="comment">// 保存新的Node数据集</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>)&#123;  <span class="comment">// 当前节点的下一个节点为null</span></span><br><span class="line">                <span class="keyword">this</span>.next = newNode; <span class="comment">// 保存当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">this</span>.next.addNode(newNode);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的成员-----------------</span></span><br><span class="line">    <span class="keyword">private</span> Node root;  <span class="comment">// 根节点</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> count; <span class="comment">// 保存数据的个数</span></span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的方法-----------------</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数据本身不具有关联特性,只有Node节点有,那么要实现关联就必须将数据包装在Node类中</span></span><br><span class="line">        Node node = <span class="keyword">new</span> Node(e); <span class="comment">// 创建一个新的节点</span></span><br><span class="line">        <span class="keyword">if</span>(root == <span class="keyword">null</span>) &#123; <span class="comment">// 现在没有根节点</span></span><br><span class="line">            <span class="keyword">this</span>.root = node; <span class="comment">// 第一个节点作为根节点</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.root.addNode(node); <span class="comment">// 将新节点保存在合适的位置</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.count ++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// return this.root == null;</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count == <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">        ILink&lt;String&gt; all = <span class="keyword">new</span> LinkImpl&lt;&gt;();</span><br><span class="line">        System.out.println(<span class="string">"[增加之前]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line">        all.add(<span class="string">"Hello"</span>);</span><br><span class="line">        all.add(<span class="string">"World"</span>);</span><br><span class="line">        System.out.println(<span class="string">"[增加之后]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="返回集合数据"><a href="#返回集合数据" class="headerlink" title="返回集合数据"></a>返回集合数据</h2><p>链表本身就属于一个动态的对象数组,那么既然是一个对象数组,那么就应该可以把所哦有的数据以<br>数组的形式返回来,那么这个时候就可以定义一个toArray()方法,但是这个时候只能够返回Object型的数组.</p>
<p><img src="http://imgs.qipo.net/image/linkedlist/toArray.jpg" alt></p>
<blockquote>
<p>1.在ILink接口里面追加新的处理方法;</p>
</blockquote>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> Object[] <span class="title">toArray</span>(<span class="params"></span>)</span>; <span class="comment">// 将集合元素以数组的形式返回</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>2.在LinkImpl里面追加有两个属性;</p>
</blockquote>
<figure class="highlight processing"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="built_in">int</span> foot; <span class="comment">// 描述的是操作数组的脚标</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">Object</span> [] returnData; <span class="comment">// 返回的数据</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>3.在Node类中递归获取数据</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> void toArrayNode () &#123;</span><br><span class="line">    LinkImpl.<span class="keyword">this</span>.returnData [LinkImpl.<span class="keyword">this</span>.foot ++] = <span class="keyword">this</span>.<span class="keyword">data</span>;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">this</span>.next != <span class="literal">null</span>) &#123; <span class="comment">// 还有下一个数据</span></span><br><span class="line">        <span class="keyword">this</span>.next.toArrayNode();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>4.在进行数据返回的时候一定要先判断是否为空集合;</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> Object[] toArray() &#123;</span><br><span class="line">    <span class="comment">// 判断是否为空</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">this</span>.root == <span class="literal">null</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">//脚标清零</span></span><br><span class="line">    <span class="keyword">this</span>.returnData =  new Object[<span class="keyword">this</span>.count]; <span class="comment">// 根据已有的长度新建一个数组</span></span><br><span class="line">    <span class="keyword">this</span>.root.toArrayNode(); <span class="comment">// 利用Node类进行递归获取数据</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.returnData;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>完整代码</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>; <span class="comment">// 增加数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>; <span class="comment">// 获取数据的个数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>; <span class="comment">// 判断是否为空集合</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray(); <span class="comment">// 将集合元素以数组的形式返回</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123; <span class="comment">// 保存的节点的数据关系</span></span><br><span class="line">        <span class="keyword">private</span> E data; <span class="comment">//保存的数据</span></span><br><span class="line">        <span class="keyword">private</span> Node next; <span class="comment">// 保存的下一个引用</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span> <span class="params">(E data)</span> </span>&#123;  <span class="comment">// 有数据的情况下才有意义</span></span><br><span class="line">            <span class="keyword">this</span>.data = data;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 第一次调用: this = LinkImpl.root;</span></span><br><span class="line">        <span class="comment">// 第二次调用: this = LinkImpl.root.next;</span></span><br><span class="line">        <span class="comment">// 第三次调用: this = LinkImpl.root.next.next;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addNode</span> <span class="params">(Node newNode)</span> </span>&#123; <span class="comment">// 保存新的Node数据集</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>)&#123;  <span class="comment">// 当前节点的下一个节点为null</span></span><br><span class="line">                <span class="keyword">this</span>.next = newNode; <span class="comment">// 保存当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">this</span>.next.addNode(newNode);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">toArrayNode</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">            LinkImpl.<span class="keyword">this</span>.returnData [LinkImpl.<span class="keyword">this</span>.foot ++] = <span class="keyword">this</span>.data;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next != <span class="keyword">null</span>) &#123; <span class="comment">// 还有下一个数据</span></span><br><span class="line">                <span class="keyword">this</span>.next.toArrayNode();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的成员-----------------</span></span><br><span class="line">    <span class="keyword">private</span> Node root;  <span class="comment">// 根节点</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> count; <span class="comment">// 保存数据的个数</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> foot; <span class="comment">// 描述的是操作数组的脚标</span></span><br><span class="line">    <span class="keyword">private</span> Object [] returnData; <span class="comment">// 返回的数据</span></span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的方法-----------------</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数据本身不具有关联特性,只有Node节点有,那么要实现关联就必须将数据包装在Node类中</span></span><br><span class="line">        Node node = <span class="keyword">new</span> Node(e); <span class="comment">// 创建一个新的节点</span></span><br><span class="line">        <span class="keyword">if</span>(root == <span class="keyword">null</span>) &#123; <span class="comment">// 现在没有根节点</span></span><br><span class="line">            <span class="keyword">this</span>.root = node; <span class="comment">// 第一个节点作为根节点</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.root.addNode(node); <span class="comment">// 将新节点保存在合适的位置</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.count ++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// return this.root == null;</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count == <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray() &#123;</span><br><span class="line">        <span class="comment">// 判断是否为空</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.root == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">//脚标清零</span></span><br><span class="line">        <span class="keyword">this</span>.returnData =  <span class="keyword">new</span> Object[<span class="keyword">this</span>.count]; <span class="comment">// 根据已有的长度新建一个数组</span></span><br><span class="line">        <span class="keyword">this</span>.root.toArrayNode(); <span class="comment">// 利用Node类进行递归获取数据</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.returnData;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">        ILink&lt;String&gt; all = <span class="keyword">new</span> LinkImpl&lt;&gt;();</span><br><span class="line">        System.out.println(<span class="string">"[增加之前]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line">        all.add(<span class="string">"Hello"</span>);</span><br><span class="line">        all.add(<span class="string">"World"</span>);</span><br><span class="line">        System.out.println(<span class="string">"[增加之后]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line"></span><br><span class="line">        Object[] result = all.toArray();</span><br><span class="line">        <span class="keyword">for</span> (Object obj : result) &#123;</span><br><span class="line">            System.out.println(obj);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>集合的数据一般如果要返回肯定要以对象数组的形式返回.</p>
<h2 id="获取指定索引数据"><a href="#获取指定索引数据" class="headerlink" title="获取指定索引数据"></a>获取指定索引数据</h2><p>链表可以像数组一样进行处理,所以也应该可以像数组一样进行索引数据的获取,在这样的情况下就可以继续利用<br>递归的形式来完成.</p>
<p><img src="http://imgs.qipo.net/image/linkedlist/getDataByIndex.png" alt></p>
<blockquote>
<p>1.在ILink接口里面追加有新的方法</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span></span>; <span class="comment">// 根据索引获取数据</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>2.在Node类中追加有根据索引获取数据的处理</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">public</span> E getNode(int index) &#123;</span><br><span class="line">    <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.<span class="keyword">data</span>;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.next.getNode(index);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>3.在LinkImpl子类里面定义数据获取的实现</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line">  <span class="keyword">public</span> E <span class="keyword">get</span>(int index) &#123;</span><br><span class="line">      <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">          <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">      &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">      <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">this</span>.root.getNode(index);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<p>这一特点是和数组很相似的，但是需要注意的是，数组获取一个数据的时间复杂度为1，而链表获取数据的时间复杂度为n.</p>
<blockquote>
<p>完整代码</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>; <span class="comment">// 增加数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>; <span class="comment">// 获取数据的个数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>; <span class="comment">// 判断是否为空集合</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray(); <span class="comment">// 将集合元素以数组的形式返回</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span></span>; <span class="comment">// 根据索引获取数据</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123; <span class="comment">// 保存的节点的数据关系</span></span><br><span class="line">        <span class="keyword">private</span> E data; <span class="comment">//保存的数据</span></span><br><span class="line">        <span class="keyword">private</span> Node next; <span class="comment">// 保存的下一个引用</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span> <span class="params">(E data)</span> </span>&#123;  <span class="comment">// 有数据的情况下才有意义</span></span><br><span class="line">            <span class="keyword">this</span>.data = data;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 第一次调用: this = LinkImpl.root;</span></span><br><span class="line">        <span class="comment">// 第二次调用: this = LinkImpl.root.next;</span></span><br><span class="line">        <span class="comment">// 第三次调用: this = LinkImpl.root.next.next;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addNode</span> <span class="params">(Node newNode)</span> </span>&#123; <span class="comment">// 保存新的Node数据集</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>)&#123;  <span class="comment">// 当前节点的下一个节点为null</span></span><br><span class="line">                <span class="keyword">this</span>.next = newNode; <span class="comment">// 保存当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">this</span>.next.addNode(newNode);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">toArrayNode</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">            LinkImpl.<span class="keyword">this</span>.returnData [LinkImpl.<span class="keyword">this</span>.foot ++] = <span class="keyword">this</span>.data;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next != <span class="keyword">null</span>) &#123; <span class="comment">// 还有下一个数据</span></span><br><span class="line">                <span class="keyword">this</span>.next.toArrayNode();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> E <span class="title">getNode</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.data;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.next.getNode(index);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的成员-----------------</span></span><br><span class="line">    <span class="keyword">private</span> Node root;  <span class="comment">// 根节点</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> count; <span class="comment">// 保存数据的个数</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> foot; <span class="comment">// 描述的是操作数组的脚标</span></span><br><span class="line">    <span class="keyword">private</span> Object [] returnData; <span class="comment">// 返回的数据</span></span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的方法-----------------</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数据本身不具有关联特性,只有Node节点有,那么要实现关联就必须将数据包装在Node类中</span></span><br><span class="line">        Node node = <span class="keyword">new</span> Node(e); <span class="comment">// 创建一个新的节点</span></span><br><span class="line">        <span class="keyword">if</span>(root == <span class="keyword">null</span>) &#123; <span class="comment">// 现在没有根节点</span></span><br><span class="line">            <span class="keyword">this</span>.root = node; <span class="comment">// 第一个节点作为根节点</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.root.addNode(node); <span class="comment">// 将新节点保存在合适的位置</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.count ++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// return this.root == null;</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count == <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray() &#123;</span><br><span class="line">        <span class="comment">// 判断是否为空</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.root == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">//脚标清零</span></span><br><span class="line">        <span class="keyword">this</span>.returnData =  <span class="keyword">new</span> Object[<span class="keyword">this</span>.count]; <span class="comment">// 根据已有的长度新建一个数组</span></span><br><span class="line">        <span class="keyword">this</span>.root.toArrayNode(); <span class="comment">// 利用Node类进行递归获取数据</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.returnData;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.root.getNode(index);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">        ILink&lt;String&gt; all = <span class="keyword">new</span> LinkImpl&lt;&gt;();</span><br><span class="line">        System.out.println(<span class="string">"[增加之前]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line">        all.add(<span class="string">"Hello"</span>);</span><br><span class="line">        all.add(<span class="string">"World"</span>);</span><br><span class="line">        System.out.println(<span class="string">"[增加之后]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line"></span><br><span class="line">        Object[] result = all.toArray();</span><br><span class="line">        <span class="keyword">for</span> (Object obj : result) &#123;</span><br><span class="line">            System.out.println(obj);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"**************************数据获取***************************"</span>);</span><br><span class="line">        System.out.println(<span class="string">"获取第一个数据: "</span> + all.get(<span class="number">0</span>));</span><br><span class="line">        System.out.println(<span class="string">"获取第二个数据: "</span> + all.get(<span class="number">1</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="修改指定索引数据"><a href="#修改指定索引数据" class="headerlink" title="修改指定索引数据"></a>修改指定索引数据</h2><p>现在已经可以根据索引来获取指定的数据了，但是既然可以获取数据，那么也可以进行数据的修改。</p>
<blockquote>
<p>1.在ILink接口中追加有新的方法</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> index,E data)</span> </span>; <span class="comment">// 修改索引数据</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>2.在Node类中应该提供有数据的修改的处理支持；</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> void setNode(int index, E <span class="keyword">data</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">        <span class="keyword">this</span>.<span class="keyword">data</span> = <span class="keyword">data</span>;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">this</span>.next.setNode(index, <span class="keyword">data</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>3.在LinkImpl的子类里面进行方法的覆写</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> void <span class="keyword">set</span>(int index, E <span class="keyword">data</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">        <span class="keyword">return</span> ;</span><br><span class="line">    &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">    <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">    <span class="keyword">this</span>.root.setNode(index, <span class="keyword">data</span>); <span class="comment">// 修改数据</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这种操作的时间复杂度也是n，因为依然需要进行数据的遍历处理。</p>
<blockquote>
<p>完整代码</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>; <span class="comment">// 增加数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>; <span class="comment">// 获取数据的个数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>; <span class="comment">// 判断是否为空集合</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray(); <span class="comment">// 将集合元素以数组的形式返回</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span></span>; <span class="comment">// 根据索引获取数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> index,E data)</span> </span>; <span class="comment">// 修改索引数据</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123; <span class="comment">// 保存的节点的数据关系</span></span><br><span class="line">        <span class="keyword">private</span> E data; <span class="comment">//保存的数据</span></span><br><span class="line">        <span class="keyword">private</span> Node next; <span class="comment">// 保存的下一个引用</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span> <span class="params">(E data)</span> </span>&#123;  <span class="comment">// 有数据的情况下才有意义</span></span><br><span class="line">            <span class="keyword">this</span>.data = data;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 第一次调用: this = LinkImpl.root;</span></span><br><span class="line">        <span class="comment">// 第二次调用: this = LinkImpl.root.next;</span></span><br><span class="line">        <span class="comment">// 第三次调用: this = LinkImpl.root.next.next;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addNode</span> <span class="params">(Node newNode)</span> </span>&#123; <span class="comment">// 保存新的Node数据集</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>)&#123;  <span class="comment">// 当前节点的下一个节点为null</span></span><br><span class="line">                <span class="keyword">this</span>.next = newNode; <span class="comment">// 保存当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">this</span>.next.addNode(newNode);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">toArrayNode</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">            LinkImpl.<span class="keyword">this</span>.returnData [LinkImpl.<span class="keyword">this</span>.foot ++] = <span class="keyword">this</span>.data;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next != <span class="keyword">null</span>) &#123; <span class="comment">// 还有下一个数据</span></span><br><span class="line">                <span class="keyword">this</span>.next.toArrayNode();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> E <span class="title">getNode</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.data;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.next.getNode(index);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNode</span><span class="params">(<span class="keyword">int</span> index, E data)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">                <span class="keyword">this</span>.data = data;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">this</span>.next.setNode(index, data);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的成员-----------------</span></span><br><span class="line">    <span class="keyword">private</span> Node root;  <span class="comment">// 根节点</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> count; <span class="comment">// 保存数据的个数</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> foot; <span class="comment">// 描述的是操作数组的脚标</span></span><br><span class="line">    <span class="keyword">private</span> Object [] returnData; <span class="comment">// 返回的数据</span></span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的方法-----------------</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数据本身不具有关联特性,只有Node节点有,那么要实现关联就必须将数据包装在Node类中</span></span><br><span class="line">        Node node = <span class="keyword">new</span> Node(e); <span class="comment">// 创建一个新的节点</span></span><br><span class="line">        <span class="keyword">if</span>(root == <span class="keyword">null</span>) &#123; <span class="comment">// 现在没有根节点</span></span><br><span class="line">            <span class="keyword">this</span>.root = node; <span class="comment">// 第一个节点作为根节点</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.root.addNode(node); <span class="comment">// 将新节点保存在合适的位置</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.count ++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// return this.root == null;</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count == <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray() &#123;</span><br><span class="line">        <span class="comment">// 判断是否为空</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.root == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">//脚标清零</span></span><br><span class="line">        <span class="keyword">this</span>.returnData =  <span class="keyword">new</span> Object[<span class="keyword">this</span>.count]; <span class="comment">// 根据已有的长度新建一个数组</span></span><br><span class="line">        <span class="keyword">this</span>.root.toArrayNode(); <span class="comment">// 利用Node类进行递归获取数据</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.returnData;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.root.getNode(index);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> index, E data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">            <span class="keyword">return</span> ;</span><br><span class="line">        &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">        <span class="keyword">this</span>.root.setNode(index, data); <span class="comment">// 修改数据</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">        ILink&lt;String&gt; all = <span class="keyword">new</span> LinkImpl&lt;&gt;();</span><br><span class="line">        System.out.println(<span class="string">"[增加之前]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line">        all.add(<span class="string">"Hello"</span>);</span><br><span class="line">        all.add(<span class="string">"World"</span>);</span><br><span class="line">        System.out.println(<span class="string">"[增加之后]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line"></span><br><span class="line">        Object[] result = all.toArray();</span><br><span class="line">        <span class="keyword">for</span> (Object obj : result) &#123;</span><br><span class="line">            System.out.println(obj);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"***********************数据修改********************"</span>);</span><br><span class="line">        all.set(<span class="number">0</span>, <span class="string">"你好"</span>);</span><br><span class="line">        System.out.println(<span class="string">"**************************数据获取***************************"</span>);</span><br><span class="line">        System.out.println(<span class="string">"获取第一个数据: "</span> + all.get(<span class="number">0</span>));</span><br><span class="line">        System.out.println(<span class="string">"获取第二个数据: "</span> + all.get(<span class="number">1</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="判断指定数据是否存在"><a href="#判断指定数据是否存在" class="headerlink" title="判断指定数据是否存在"></a>判断指定数据是否存在</h2><p>在一个集合里面往往会保存有大量的数据，有些时候需要判断某个数据是否存在，这个时候就可以<br>通过对象比较的模式（equals方法）来完成判断.</p>
<blockquote>
<p>1.在ILink接口中追加判断的方法</p>
</blockquote>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">boolean</span> <span class="title">contains</span><span class="params">(E data)</span></span>; <span class="comment">// 判断数据 是否存在</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>2.在Node类中进行依次判断</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">public</span> boolean containNode(E <span class="keyword">data</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">this</span>.<span class="keyword">data</span>.equals(<span class="keyword">data</span>)) &#123; <span class="comment">// 对象比较</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="literal">null</span>) &#123; <span class="comment">// 找不到后续节点</span></span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;  <span class="comment">// 找不到</span></span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.next.containNode(<span class="keyword">data</span>); <span class="comment">// 向后继续寻找</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>3.在LinkImpl子类里面实现此方法</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line">  <span class="keyword">public</span> boolean contains(E <span class="keyword">data</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span>(<span class="keyword">data</span> == <span class="literal">null</span>) &#123;</span><br><span class="line">          <span class="keyword">return</span> <span class="literal">false</span>; <span class="comment">// 没有数据</span></span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">this</span>.root.containNode(<span class="keyword">data</span>); <span class="comment">// 交给Node类判断</span></span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>完整代码</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>; <span class="comment">// 增加数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>; <span class="comment">// 获取数据的个数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>; <span class="comment">// 判断是否为空集合</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray(); <span class="comment">// 将集合元素以数组的形式返回</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span></span>; <span class="comment">// 根据索引获取数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> index,E data)</span> </span>; <span class="comment">// 修改索引数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">contains</span><span class="params">(E data)</span></span>; <span class="comment">// 判断数据 是否存在</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123; <span class="comment">// 保存的节点的数据关系</span></span><br><span class="line">        <span class="keyword">private</span> E data; <span class="comment">//保存的数据</span></span><br><span class="line">        <span class="keyword">private</span> Node next; <span class="comment">// 保存的下一个引用</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span> <span class="params">(E data)</span> </span>&#123;  <span class="comment">// 有数据的情况下才有意义</span></span><br><span class="line">            <span class="keyword">this</span>.data = data;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 第一次调用: this = LinkImpl.root;</span></span><br><span class="line">        <span class="comment">// 第二次调用: this = LinkImpl.root.next;</span></span><br><span class="line">        <span class="comment">// 第三次调用: this = LinkImpl.root.next.next;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addNode</span> <span class="params">(Node newNode)</span> </span>&#123; <span class="comment">// 保存新的Node数据集</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>)&#123;  <span class="comment">// 当前节点的下一个节点为null</span></span><br><span class="line">                <span class="keyword">this</span>.next = newNode; <span class="comment">// 保存当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">this</span>.next.addNode(newNode);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">toArrayNode</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">            LinkImpl.<span class="keyword">this</span>.returnData [LinkImpl.<span class="keyword">this</span>.foot ++] = <span class="keyword">this</span>.data;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next != <span class="keyword">null</span>) &#123; <span class="comment">// 还有下一个数据</span></span><br><span class="line">                <span class="keyword">this</span>.next.toArrayNode();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> E <span class="title">getNode</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.data;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.next.getNode(index);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNode</span><span class="params">(<span class="keyword">int</span> index, E data)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">                <span class="keyword">this</span>.data = data;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">this</span>.next.setNode(index, data);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">containNode</span><span class="params">(E data)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.data.equals(data)) &#123; <span class="comment">// 对象比较</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>) &#123; <span class="comment">// 找不到后续节点</span></span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">false</span>;  <span class="comment">// 找不到</span></span><br><span class="line">                &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">this</span>.next.containNode(data); <span class="comment">// 向后继续寻找</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的成员-----------------</span></span><br><span class="line">    <span class="keyword">private</span> Node root;  <span class="comment">// 根节点</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> count; <span class="comment">// 保存数据的个数</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> foot; <span class="comment">// 描述的是操作数组的脚标</span></span><br><span class="line">    <span class="keyword">private</span> Object [] returnData; <span class="comment">// 返回的数据</span></span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的方法-----------------</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数据本身不具有关联特性,只有Node节点有,那么要实现关联就必须将数据包装在Node类中</span></span><br><span class="line">        Node node = <span class="keyword">new</span> Node(e); <span class="comment">// 创建一个新的节点</span></span><br><span class="line">        <span class="keyword">if</span>(root == <span class="keyword">null</span>) &#123; <span class="comment">// 现在没有根节点</span></span><br><span class="line">            <span class="keyword">this</span>.root = node; <span class="comment">// 第一个节点作为根节点</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.root.addNode(node); <span class="comment">// 将新节点保存在合适的位置</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.count ++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// return this.root == null;</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count == <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray() &#123;</span><br><span class="line">        <span class="comment">// 判断是否为空</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.root == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">//脚标清零</span></span><br><span class="line">        <span class="keyword">this</span>.returnData =  <span class="keyword">new</span> Object[<span class="keyword">this</span>.count]; <span class="comment">// 根据已有的长度新建一个数组</span></span><br><span class="line">        <span class="keyword">this</span>.root.toArrayNode(); <span class="comment">// 利用Node类进行递归获取数据</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.returnData;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.root.getNode(index);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> index, E data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">            <span class="keyword">return</span> ;</span><br><span class="line">        &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">        <span class="keyword">this</span>.root.setNode(index, data); <span class="comment">// 修改数据</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">contains</span><span class="params">(E data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(data == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>; <span class="comment">// 没有数据</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.root.containNode(data); <span class="comment">// 交给Node类判断</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">        ILink&lt;String&gt; all = <span class="keyword">new</span> LinkImpl&lt;&gt;();</span><br><span class="line">        System.out.println(<span class="string">"[增加之前]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line">        all.add(<span class="string">"Hello"</span>);</span><br><span class="line">        all.add(<span class="string">"World"</span>);</span><br><span class="line">        System.out.println(<span class="string">"[增加之后]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line"></span><br><span class="line">        Object[] result = all.toArray();</span><br><span class="line">        <span class="keyword">for</span> (Object obj : result) &#123;</span><br><span class="line">            System.out.println(obj);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"***********************数据修改********************"</span>);</span><br><span class="line">        all.set(<span class="number">0</span>, <span class="string">"你好"</span>);</span><br><span class="line">        System.out.println(<span class="string">"**************************数据获取***************************"</span>);</span><br><span class="line">        System.out.println(<span class="string">"获取第一个数据: "</span> + all.get(<span class="number">0</span>));</span><br><span class="line">        System.out.println(<span class="string">"获取第二个数据: "</span> + all.get(<span class="number">1</span>));</span><br><span class="line">        System.out.println(<span class="string">"*************************数据包含判断********************"</span>);</span><br><span class="line">        System.out.println(<span class="string">"数据是否包含World:"</span> + all.contains(<span class="string">"World"</span>));</span><br><span class="line">        System.out.println(<span class="string">"数据是否包含hello:"</span> + all.contains(<span class="string">"hello"</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于整个链表没有null数据的存在，所以整体的程序在判断的时候直接使用每一个的节点数据发出equals()<br>方法调用即可。</p>
<p>##　数据的删除处理</p>
<p>数据的删除指的是可以从集合里面删除掉指定的一个数据内容，也就是说此时传递的是数据内容，<br>如果要实现这种操作依然需要对象比较的支持。但是对于集合数据的删除我们需要考虑两种情况：</p>
<ul>
<li>要删除是根节点数据(LinkImpl与根节点有关，所以这个判断由根节点完成)；</li>
</ul>
<p><img src="http://imgs.qipo.net/image/linkedlist/deleteData.png" alt></p>
<ul>
<li>要删除的不是根节点数据（由Node类负责）；</li>
</ul>
<p><img src="http://imgs.qipo.net/image/linkedlist/deleteData1.png" alt></p>
<blockquote>
<p>1.在ILink接口里面追加新的删除方法</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">remove</span><span class="params">(E data)</span></span>; <span class="comment">// 数据删除</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>2.在LinkImpl里面实现根节点的判断</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line">  <span class="keyword">public</span> void remove(E <span class="keyword">data</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span>(<span class="keyword">this</span>.contains(<span class="keyword">data</span>))&#123; <span class="comment">// 判断数据是否存在</span></span><br><span class="line">          <span class="keyword">if</span>(<span class="keyword">data</span>.equals(<span class="keyword">this</span>.root.<span class="keyword">data</span>)) &#123; <span class="comment">// 要删除节点为根节点</span></span><br><span class="line">              <span class="keyword">this</span>.root = <span class="keyword">this</span>.root.next;  <span class="comment">// 根的下一个节点</span></span><br><span class="line">          &#125;</span><br><span class="line">          <span class="keyword">this</span>.count --;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>3.如果要删除节点不为根节点就需要进行后续节点判断，但是请一定要记住，此时根节点已经判断完成了，再判断应该从根节点的下一个开始判断，在Node类中追加删除处理；</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> void removeNode(Node previous, E <span class="keyword">data</span>) &#123;</span><br><span class="line">           <span class="keyword">if</span>(<span class="keyword">data</span>.equals(<span class="keyword">this</span>.<span class="keyword">data</span>)) &#123;</span><br><span class="line">               previous.next = <span class="keyword">this</span>.next; <span class="comment">// 空出当前节点</span></span><br><span class="line">           &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">               <span class="keyword">if</span>(<span class="keyword">this</span>.next != <span class="literal">null</span>) &#123;  <span class="comment">// 有后续节点</span></span><br><span class="line">                   <span class="keyword">this</span>.next.removeNode(<span class="keyword">this</span>, <span class="keyword">data</span>); <span class="comment">//向后继续删除</span></span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>4.完善LinkImpl子类中的方法</p>
</blockquote>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line">   <span class="keyword">public</span> void remove(E <span class="keyword">data</span>) &#123;</span><br><span class="line">       <span class="keyword">if</span>(<span class="keyword">this</span>.contains(<span class="keyword">data</span>))&#123; <span class="comment">// 判断数据是否存在</span></span><br><span class="line">           <span class="keyword">if</span>(<span class="keyword">data</span>.equals(<span class="keyword">this</span>.root.<span class="keyword">data</span>)) &#123; <span class="comment">// 要删除节点为根节点</span></span><br><span class="line">               <span class="keyword">this</span>.root = <span class="keyword">this</span>.root.next;  <span class="comment">// 根的下一个节点</span></span><br><span class="line">           &#125;<span class="keyword">else</span> &#123; <span class="comment">// 交由Node类负责删除</span></span><br><span class="line">               <span class="keyword">this</span>.root.next.removeNode(<span class="keyword">this</span>.root, <span class="keyword">data</span>);</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="keyword">this</span>.count --;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>完整代码</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>; <span class="comment">// 增加数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>; <span class="comment">// 获取数据的个数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>; <span class="comment">// 判断是否为空集合</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray(); <span class="comment">// 将集合元素以数组的形式返回</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span></span>; <span class="comment">// 根据索引获取数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> index,E data)</span> </span>; <span class="comment">// 修改索引数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">contains</span><span class="params">(E data)</span></span>; <span class="comment">// 判断数据 是否存在</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span>  <span class="keyword">void</span> <span class="title">remove</span><span class="params">(E data)</span></span>; <span class="comment">// 数据删除</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123; <span class="comment">// 保存的节点的数据关系</span></span><br><span class="line">        <span class="keyword">private</span> E data; <span class="comment">//保存的数据</span></span><br><span class="line">        <span class="keyword">private</span> Node next; <span class="comment">// 保存的下一个引用</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span> <span class="params">(E data)</span> </span>&#123;  <span class="comment">// 有数据的情况下才有意义</span></span><br><span class="line">            <span class="keyword">this</span>.data = data;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 第一次调用: this = LinkImpl.root;</span></span><br><span class="line">        <span class="comment">// 第二次调用: this = LinkImpl.root.next;</span></span><br><span class="line">        <span class="comment">// 第三次调用: this = LinkImpl.root.next.next;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addNode</span> <span class="params">(Node newNode)</span> </span>&#123; <span class="comment">// 保存新的Node数据集</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>)&#123;  <span class="comment">// 当前节点的下一个节点为null</span></span><br><span class="line">                <span class="keyword">this</span>.next = newNode; <span class="comment">// 保存当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">this</span>.next.addNode(newNode);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">toArrayNode</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">            LinkImpl.<span class="keyword">this</span>.returnData [LinkImpl.<span class="keyword">this</span>.foot ++] = <span class="keyword">this</span>.data;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next != <span class="keyword">null</span>) &#123; <span class="comment">// 还有下一个数据</span></span><br><span class="line">                <span class="keyword">this</span>.next.toArrayNode();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> E <span class="title">getNode</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.data;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.next.getNode(index);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNode</span><span class="params">(<span class="keyword">int</span> index, E data)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">                <span class="keyword">this</span>.data = data;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">this</span>.next.setNode(index, data);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">containNode</span><span class="params">(E data)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(data.equals(<span class="keyword">this</span>.data)) &#123; <span class="comment">// 对象比较</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>) &#123; <span class="comment">// 找不到后续节点</span></span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">false</span>;  <span class="comment">// 找不到</span></span><br><span class="line">                &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">this</span>.next.containNode(data); <span class="comment">// 向后继续寻找</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">removeNode</span><span class="params">(Node previous, E data)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(data.equals(<span class="keyword">this</span>.data)) &#123;</span><br><span class="line">                previous.next = <span class="keyword">this</span>.next; <span class="comment">// 空出当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">if</span>(<span class="keyword">this</span>.next != <span class="keyword">null</span>) &#123;  <span class="comment">// 有后续节点</span></span><br><span class="line">                    <span class="keyword">this</span>.next.removeNode(<span class="keyword">this</span>, data); <span class="comment">//向后继续删除</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的成员-----------------</span></span><br><span class="line">    <span class="keyword">private</span> Node root;  <span class="comment">// 根节点</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> count; <span class="comment">// 保存数据的个数</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> foot; <span class="comment">// 描述的是操作数组的脚标</span></span><br><span class="line">    <span class="keyword">private</span> Object [] returnData; <span class="comment">// 返回的数据</span></span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的方法-----------------</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数据本身不具有关联特性,只有Node节点有,那么要实现关联就必须将数据包装在Node类中</span></span><br><span class="line">        Node node = <span class="keyword">new</span> Node(e); <span class="comment">// 创建一个新的节点</span></span><br><span class="line">        <span class="keyword">if</span>(root == <span class="keyword">null</span>) &#123; <span class="comment">// 现在没有根节点</span></span><br><span class="line">            <span class="keyword">this</span>.root = node; <span class="comment">// 第一个节点作为根节点</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.root.addNode(node); <span class="comment">// 将新节点保存在合适的位置</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.count ++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// return this.root == null;</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count == <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray() &#123;</span><br><span class="line">        <span class="comment">// 判断是否为空</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.root == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">//脚标清零</span></span><br><span class="line">        <span class="keyword">this</span>.returnData =  <span class="keyword">new</span> Object[<span class="keyword">this</span>.count]; <span class="comment">// 根据已有的长度新建一个数组</span></span><br><span class="line">        <span class="keyword">this</span>.root.toArrayNode(); <span class="comment">// 利用Node类进行递归获取数据</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.returnData;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.root.getNode(index);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> index, E data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">            <span class="keyword">return</span> ;</span><br><span class="line">        &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">        <span class="keyword">this</span>.root.setNode(index, data); <span class="comment">// 修改数据</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">contains</span><span class="params">(E data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(data == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>; <span class="comment">// 没有数据</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.root.containNode(data); <span class="comment">// 交给Node类判断</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">remove</span><span class="params">(E data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.contains(data))&#123; <span class="comment">// 判断数据是否存在</span></span><br><span class="line">            <span class="keyword">if</span>(data.equals(<span class="keyword">this</span>.root.data)) &#123; <span class="comment">// 要删除节点为根节点</span></span><br><span class="line">                <span class="keyword">this</span>.root = <span class="keyword">this</span>.root.next;  <span class="comment">// 根的下一个节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span> &#123; <span class="comment">// 交由Node类负责删除</span></span><br><span class="line">                <span class="keyword">this</span>.root.next.removeNode(<span class="keyword">this</span>.root, data);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">this</span>.count --;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">        ILink&lt;String&gt; all = <span class="keyword">new</span> LinkImpl&lt;&gt;();</span><br><span class="line">        System.out.println(<span class="string">"[增加之前]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line">        all.add(<span class="string">"Hello"</span>);</span><br><span class="line">        all.add(<span class="string">"World"</span>);</span><br><span class="line">        System.out.println(<span class="string">"[增加之后]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line"></span><br><span class="line">        Object[] result = all.toArray();</span><br><span class="line">        <span class="keyword">for</span> (Object obj : result) &#123;</span><br><span class="line">            System.out.println(obj);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"***********************数据修改********************"</span>);</span><br><span class="line">        all.set(<span class="number">0</span>, <span class="string">"你好"</span>);</span><br><span class="line">        System.out.println(<span class="string">"**************************数据获取***************************"</span>);</span><br><span class="line">        System.out.println(<span class="string">"获取第一个数据: "</span> + all.get(<span class="number">0</span>));</span><br><span class="line">        System.out.println(<span class="string">"获取第二个数据: "</span> + all.get(<span class="number">1</span>));</span><br><span class="line">        System.out.println(<span class="string">"*************************数据包含判断********************"</span>);</span><br><span class="line">        System.out.println(<span class="string">"数据是否包含World:"</span> + all.contains(<span class="string">"World"</span>));</span><br><span class="line">        System.out.println(<span class="string">"数据是否包含hello:"</span> + all.contains(<span class="string">"hello"</span>));</span><br><span class="line">        System.out.println(<span class="string">"*************************删除数据********************"</span>);</span><br><span class="line">        all.remove(<span class="string">"World"</span>);</span><br><span class="line">        System.out.println(<span class="string">"数据是否包含World:"</span> + all.contains(<span class="string">"World"</span>));</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>删除的逻辑就是用了引用改变。</p>
<h2 id="清空链表"><a href="#清空链表" class="headerlink" title="清空链表"></a>清空链表</h2><p>有的时候需要进行链表的整体清空处理，这个时候结可以直接根据根元素来进行控制，只要root设置为了null，那么后续的节点就都不存在了。</p>
<blockquote>
<p>1.在ILink接口里面追加有清空处理方法；<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">clean</span><span class="params">()</span></span>; <span class="comment">// 数据的清空</span></span><br></pre></td></tr></table></figure></p>
</blockquote>
<blockquote>
<p>2.在LinkImpl子类里覆写方法</p>
</blockquote>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">clean</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.root = <span class="keyword">null</span>; <span class="comment">// 后续的节点都没有了</span></span><br><span class="line">    <span class="keyword">this</span>.count = <span class="number">0</span>;  <span class="comment">// 个数清零</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>完整代码</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 链表;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123; <span class="comment">// 设置泛型避免安全隐患</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span></span>; <span class="comment">// 增加数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>; <span class="comment">// 获取数据的个数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span></span>; <span class="comment">// 判断是否为空集合</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray(); <span class="comment">// 将集合元素以数组的形式返回</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span></span>; <span class="comment">// 根据索引获取数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> index,E data)</span> </span>; <span class="comment">// 修改索引数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">contains</span><span class="params">(E data)</span></span>; <span class="comment">// 判断数据 是否存在</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span>  <span class="keyword">void</span> <span class="title">remove</span><span class="params">(E data)</span></span>; <span class="comment">// 数据删除</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">clean</span><span class="params">()</span></span>; <span class="comment">// 数据的清空</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">ILink</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123; <span class="comment">// 保存的节点的数据关系</span></span><br><span class="line">        <span class="keyword">private</span> E data; <span class="comment">//保存的数据</span></span><br><span class="line">        <span class="keyword">private</span> Node next; <span class="comment">// 保存的下一个引用</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Node</span> <span class="params">(E data)</span> </span>&#123;  <span class="comment">// 有数据的情况下才有意义</span></span><br><span class="line">            <span class="keyword">this</span>.data = data;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 第一次调用: this = LinkImpl.root;</span></span><br><span class="line">        <span class="comment">// 第二次调用: this = LinkImpl.root.next;</span></span><br><span class="line">        <span class="comment">// 第三次调用: this = LinkImpl.root.next.next;</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addNode</span> <span class="params">(Node newNode)</span> </span>&#123; <span class="comment">// 保存新的Node数据集</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>)&#123;  <span class="comment">// 当前节点的下一个节点为null</span></span><br><span class="line">                <span class="keyword">this</span>.next = newNode; <span class="comment">// 保存当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">this</span>.next.addNode(newNode);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">toArrayNode</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">            LinkImpl.<span class="keyword">this</span>.returnData [LinkImpl.<span class="keyword">this</span>.foot ++] = <span class="keyword">this</span>.data;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.next != <span class="keyword">null</span>) &#123; <span class="comment">// 还有下一个数据</span></span><br><span class="line">                <span class="keyword">this</span>.next.toArrayNode();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> E <span class="title">getNode</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.data;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">this</span>.next.getNode(index);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setNode</span><span class="params">(<span class="keyword">int</span> index, E data)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(LinkImpl.<span class="keyword">this</span>.foot ++ == index) &#123; <span class="comment">// 索引相同</span></span><br><span class="line">                <span class="keyword">this</span>.data = data;  <span class="comment">// 要返回当前数据</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">this</span>.next.setNode(index, data);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">containNode</span><span class="params">(E data)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(data.equals(<span class="keyword">this</span>.data)) &#123; <span class="comment">// 对象比较</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">if</span>(<span class="keyword">this</span>.next == <span class="keyword">null</span>) &#123; <span class="comment">// 找不到后续节点</span></span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">false</span>;  <span class="comment">// 找不到</span></span><br><span class="line">                &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="keyword">this</span>.next.containNode(data); <span class="comment">// 向后继续寻找</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">removeNode</span><span class="params">(Node previous, E data)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(data.equals(<span class="keyword">this</span>.data)) &#123;</span><br><span class="line">                previous.next = <span class="keyword">this</span>.next; <span class="comment">// 空出当前节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">if</span>(<span class="keyword">this</span>.next != <span class="keyword">null</span>) &#123;  <span class="comment">// 有后续节点</span></span><br><span class="line">                    <span class="keyword">this</span>.next.removeNode(<span class="keyword">this</span>, data); <span class="comment">//向后继续删除</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的成员-----------------</span></span><br><span class="line">    <span class="keyword">private</span> Node root;  <span class="comment">// 根节点</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> count; <span class="comment">// 保存数据的个数</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> foot; <span class="comment">// 描述的是操作数组的脚标</span></span><br><span class="line">    <span class="keyword">private</span> Object [] returnData; <span class="comment">// 返回的数据</span></span><br><span class="line">    <span class="comment">// ---------------以下为Link类中定义的方法-----------------</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 数据本身不具有关联特性,只有Node节点有,那么要实现关联就必须将数据包装在Node类中</span></span><br><span class="line">        Node node = <span class="keyword">new</span> Node(e); <span class="comment">// 创建一个新的节点</span></span><br><span class="line">        <span class="keyword">if</span>(root == <span class="keyword">null</span>) &#123; <span class="comment">// 现在没有根节点</span></span><br><span class="line">            <span class="keyword">this</span>.root = node; <span class="comment">// 第一个节点作为根节点</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.root.addNode(node); <span class="comment">// 将新节点保存在合适的位置</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.count ++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEmpty</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// return this.root == null;</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.count == <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Object[] toArray() &#123;</span><br><span class="line">        <span class="comment">// 判断是否为空</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.root == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">//脚标清零</span></span><br><span class="line">        <span class="keyword">this</span>.returnData =  <span class="keyword">new</span> Object[<span class="keyword">this</span>.count]; <span class="comment">// 根据已有的长度新建一个数组</span></span><br><span class="line">        <span class="keyword">this</span>.root.toArrayNode(); <span class="comment">// 利用Node类进行递归获取数据</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.returnData;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> E <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.root.getNode(index);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> index, E data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &gt;= <span class="keyword">this</span>.count) &#123; <span class="comment">// 索引应该在指定范围之内</span></span><br><span class="line">            <span class="keyword">return</span> ;</span><br><span class="line">        &#125; <span class="comment">// 索引数据的获取应该由Node来完成</span></span><br><span class="line">        <span class="keyword">this</span>.foot = <span class="number">0</span>; <span class="comment">// 重置角标</span></span><br><span class="line">        <span class="keyword">this</span>.root.setNode(index, data); <span class="comment">// 修改数据</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">contains</span><span class="params">(E data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(data == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>; <span class="comment">// 没有数据</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.root.containNode(data); <span class="comment">// 交给Node类判断</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">remove</span><span class="params">(E data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.contains(data))&#123; <span class="comment">// 判断数据是否存在</span></span><br><span class="line">            <span class="keyword">if</span>(data.equals(<span class="keyword">this</span>.root.data)) &#123; <span class="comment">// 要删除节点为根节点</span></span><br><span class="line">                <span class="keyword">this</span>.root = <span class="keyword">this</span>.root.next;  <span class="comment">// 根的下一个节点</span></span><br><span class="line">            &#125;<span class="keyword">else</span> &#123; <span class="comment">// 交由Node类负责删除</span></span><br><span class="line">                <span class="keyword">this</span>.root.next.removeNode(<span class="keyword">this</span>.root, data);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">this</span>.count --;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">clean</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.root = <span class="keyword">null</span>; <span class="comment">// 后续的节点都没有了</span></span><br><span class="line">        <span class="keyword">this</span>.count = <span class="number">0</span>;  <span class="comment">// 个数清零</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LinkDemo2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;</span><br><span class="line">        ILink&lt;String&gt; all = <span class="keyword">new</span> LinkImpl&lt;&gt;();</span><br><span class="line">        System.out.println(<span class="string">"[增加之前]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line">        all.add(<span class="string">"Hello"</span>);</span><br><span class="line">        all.add(<span class="string">"World"</span>);</span><br><span class="line">        System.out.println(<span class="string">"[增加之后]数据的个数:"</span> + all.size());</span><br><span class="line">        System.out.println(<span class="string">"是否为空集合:"</span> + all.isEmpty());</span><br><span class="line"></span><br><span class="line">        Object[] result = all.toArray();</span><br><span class="line">        <span class="keyword">for</span> (Object obj : result) &#123;</span><br><span class="line">            System.out.println(obj);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"***********************数据修改********************"</span>);</span><br><span class="line">        all.set(<span class="number">0</span>, <span class="string">"你好"</span>);</span><br><span class="line">        System.out.println(<span class="string">"**************************数据获取***************************"</span>);</span><br><span class="line">        System.out.println(<span class="string">"获取第一个数据: "</span> + all.get(<span class="number">0</span>));</span><br><span class="line">        System.out.println(<span class="string">"获取第二个数据: "</span> + all.get(<span class="number">1</span>));</span><br><span class="line">        System.out.println(<span class="string">"*************************数据包含判断********************"</span>);</span><br><span class="line">        System.out.println(<span class="string">"数据是否包含World:"</span> + all.contains(<span class="string">"World"</span>));</span><br><span class="line">        System.out.println(<span class="string">"数据是否包含hello:"</span> + all.contains(<span class="string">"hello"</span>));</span><br><span class="line">        System.out.println(<span class="string">"*************************删除数据********************"</span>);</span><br><span class="line">        all.remove(<span class="string">"World"</span>);</span><br><span class="line">        System.out.println(<span class="string">"数据是否包含World:"</span> + all.contains(<span class="string">"World"</span>));</span><br><span class="line">        System.out.println(<span class="string">"*************************清空数据********************"</span>);</span><br><span class="line">        all.clean();</span><br><span class="line">        System.out.println(<span class="string">"数据的个数:"</span> + all.size());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>以上就是最简单最基础的单向链表的实现。</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div></div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Key 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Key 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Java/" rel="tag"># Java</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/06/07/function/" rel="next" title="Java函数式编程">
                <i class="fa fa-chevron-left"></i> Java函数式编程
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/06/12/Thread/" rel="prev" title="Java多线程编程">
                Java多线程编程 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="Key">
            
              <p class="site-author-name" itemprop="name">Key</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">107</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/keyarea" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:wxkayle@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#链表"><span class="nav-number">1.</span> <span class="nav-text">链表</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#链表的基本定义"><span class="nav-number">1.1.</span> <span class="nav-text">链表的基本定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据增加"><span class="nav-number">1.2.</span> <span class="nav-text">数据增加</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取集合个数"><span class="nav-number">1.3.</span> <span class="nav-text">获取集合个数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#空集合判断"><span class="nav-number">1.4.</span> <span class="nav-text">空集合判断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#返回集合数据"><span class="nav-number">1.5.</span> <span class="nav-text">返回集合数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取指定索引数据"><span class="nav-number">1.6.</span> <span class="nav-text">获取指定索引数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改指定索引数据"><span class="nav-number">1.7.</span> <span class="nav-text">修改指定索引数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#判断指定数据是否存在"><span class="nav-number">1.8.</span> <span class="nav-text">判断指定数据是否存在</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#清空链表"><span class="nav-number">1.9.</span> <span class="nav-text">清空链表</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-snowflake-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Key</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">190.8k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
